<?php
/**
 * Created by PhpStorm.
 * User: micoya
 * Date: 2018/11/29
 * Time: 2:21 PM
 */

namespace app\index\controller;


use app\common\base\AuthenticatedController;
use app\common\base\Form;
use think\Db;

class DeviceController extends AuthenticatedController {


    public function GetList() {
        $classify = Db::table('device_classify')->select();

        foreach ($classify as $key => $value) {
            $list = Db::table('device')->where('device_classify_id', $value['id'])->select();
            $classify[$key]['device_list'] = $list;
        }

        return Success('获取成功', $classify);
    }


    public function GetOne() {
        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $result = Db::table('device')->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        $result = $result->getData();

        // 附加item
        $item = Db::table('device_item')->where('device_id', $result['id'])->select();
        $result['item'] = $item;

        return Success('获取成功', $result);

    }

}